今天來實作使用ActivityResultContracts.TakePicture()
開啟相機,拍照後把照片儲存在Android/data/packageName/files/Pictures/001.jpg
,因為是存在packageName
裡面,所以不用寫權限相關的程式碼,非常方便。
開一個新的Activity來玩,首先一樣是頁面的部份
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CropLensActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/aclMbCreatePackageNamePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:text="在應用程式資料夾建立照片"
android:textAllCaps="false"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/aclIvPackageNamePicture"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/aclIvPackageNamePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="4dp"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aclMbCreatePackageNamePicture"
tools:src="@tools:sample/backgrounds/scenic" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
再來是點擊拍照的程式碼
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_crop_lens)
aclMbCreatePackageNamePicture.setOnClickListener {
val packageNameFile =
File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "001.jpg")
val uri = getPictureUri(packageNameFile)
takePictureResultLauncher.launch(uri)
}
}
private fun getPictureUri(picturePath: File): Uri? {
return FileProvider.getUriForFile(
this,
"$packageName${BaseConstants.DOT_FILEPROVIDER}",
picturePath
)
}
啟動器的部份,確定相片儲存成功後,把相片的uri拿來顯示
private val takePictureResultLauncher =
registerForActivityResult(ActivityResultContracts.TakePicture()) { isTakePicture ->
if (!isTakePicture) {
Log.d("maho", "拍照建立檔案失敗")
return@registerForActivityResult
}
val packageNameFile =
File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "001.jpg")
MediaScannerConnection.scanFile(
this,
arrayOf(packageNameFile.toString(), phoneFile.toString()),
null
) { s, uri ->
Log.d("maho", "s1: $s / uri1: $uri")
}
aclIvPackageNamePicture.setImageURI(getPictureUri(packageNameFile))
}
這樣寫完跟以前比起來少了很多程式碼,舒服。